package system;

import gui.DrawingPanel;

import java.awt.image.BufferedImage;

/**
 * @author Kevin Peters
 */
public class DrawingOperation {

	private DrawingPanel alteredPanel;
	private int imageX;
	private int imageY;
	private int colorValueBefore;
	private int colorValueAfter;

	public int getImageX() {
		return imageX;
	}

	public void setImageX(int imageX) {
		this.imageX = imageX;
	}

	public int getImageY() {
		return imageY;
	}

	public void setImageY(int imageY) {
		this.imageY = imageY;
	}

	public int getColorBefore() {
		return colorValueBefore;
	}

	public void setColorBefore(int colorBefore) {
		this.colorValueBefore = colorBefore;
	}

	public int getColorAfter() {
		return colorValueAfter;
	}

	public void setColorAfter(int colorAfter) {
		this.colorValueAfter = colorAfter;
	}

	public DrawingOperation(DrawingPanel alteredPanel, int imageX, int imageY,
			int colorValueBefore, int colorValueAfter) {
		super();
		this.alteredPanel = alteredPanel;
		this.imageX = imageX;
		this.imageY = imageY;
		this.colorValueBefore = colorValueBefore;
		this.colorValueAfter = colorValueAfter;
	}

	public DrawingPanel getAlteredPanel() {
		return alteredPanel;
	}

	public void setAlteredPanel(DrawingPanel alteredPanel) {
		this.alteredPanel = alteredPanel;
	}

	/**
	 * Reverts the last action
	 */
	public void undo() {
		ImagePart imagePart = alteredPanel.getImagePart();
		BufferedImage panelImage = imagePart.getImage();
		panelImage.setRGB(imageX, imageY, colorValueBefore);
		alteredPanel.repaint();
	}

	/**
	 * Reverts the last action
	 */
	public void redo() {
		ImagePart imagePart = alteredPanel.getImagePart();
		BufferedImage panelImage = imagePart.getImage();
		panelImage.setRGB(imageX, imageY, colorValueAfter);
		alteredPanel.repaint();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((alteredPanel == null) ? 0 : alteredPanel.hashCode());
		result = prime * result + colorValueAfter;
		result = prime * result + colorValueBefore;
		result = prime * result + imageX;
		result = prime * result + imageY;
		return result;
	}

	/**
	 * removed colorBefore because of multiple paintings on the same rectangle (then
	 * colorValueBefore changes but they have to be equal anyway)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		DrawingOperation other = (DrawingOperation) obj;
		if (alteredPanel == null) {
			if (other.alteredPanel != null)
				return false;
		} else if (!alteredPanel.equals(other.alteredPanel))
			return false;
		if (colorValueAfter != other.colorValueAfter)
			return false;
		if (imageX != other.imageX)
			return false;
		if (imageY != other.imageY)
			return false;
		return true;
	}

}
